package com.xiaomazi.edu.controller;


import com.xiaomazi.common.utils.R;
import com.xiaomazi.edu.entity.Chapter;
import com.xiaomazi.edu.service.ChapterService;
import com.xiaomazi.edu.vo.ChapterResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: llxy-parent
 * @description: 章节前端控制器
 * @author: 小马子
 * @create: 2023-02-18 20:51
 **/
@RestController
@RequestMapping("/eduservice/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;


    @GetMapping("/getChapterInfoByCourseId/{courseId}")
    public R getChapterInfoByCourseId(@PathVariable String courseId){
        List<ChapterResult> chapterResultList =  chapterService.getChapterInfoByCourseId(courseId);
        return R.ok().data("chapterResult",chapterResultList);
    }

    @GetMapping("/getChapterInfoById/{chapterId}")
    public R getChapterInfoById(@PathVariable String chapterId){
        return R.ok().data("chapterInfo",chapterService.getById(chapterId));
    }

    @PostMapping("/addChapter")
    public R addChapter(@RequestBody Chapter chapter) {
        return chapterService.save(chapter) ? R.ok() : R.error().data("msg", "添加章节失败");
    }

    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {
        return chapterService.updateById(chapter) ? R.ok() : R.error().data("msg", "修改章节失败");
    }

    @DeleteMapping("/removeChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        return chapterService.removeChapterAndVideo(chapterId);
    }






}
